﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using System.Data.Common;

namespace MiniFox.Data.Mapping
{
    /// <summary>
    /// 
    /// </summary>
    public class TableMappingCollection : List<ITableMapping>, ITableMappingCollection
    {
        internal TableMappingCollection()
        { }
        #region ITableMappingCollection 成员
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceTableName"></param>
        /// <param name="dataSetTableName"></param>
        /// <returns></returns>
        public ITableMapping Add(string sourceTableName, string dataSetTableName)
        {
            TableMapping tm = new TableMapping(sourceTableName, dataSetTableName);
            this.Add(tm);
            return tm;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceTableName"></param>
        /// <returns></returns>
        public bool Contains(string sourceTableName)
        {
            return this.Exists(p=>p.SourceTable == sourceTableName);
            //return this.Exists(delegate(ITableMapping tm) { return tm.SourceTable == sourceTableName; });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataSetTableName"></param>
        /// <returns></returns>
        public ITableMapping GetByDataSetTable(string dataSetTableName)
        {
            return this.Find(p => p.DataSetTable == dataSetTableName);
            //return this.Find(delegate(ITableMapping tm) { return tm.DataSetTable == dataSetTableName; });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceTableName"></param>
        /// <returns></returns>
        public ITableMapping GetBySourceTable(string sourceTableName)
        {
            return this.Find(p => p.SourceTable == sourceTableName);
            //return this.Find(delegate(ITableMapping tm) { return tm.SourceTable == sourceTableName; });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceTableName"></param>
        /// <returns></returns>
        public int IndexOf(string sourceTableName)
        {
            return this.IndexOf(GetBySourceTable(sourceTableName));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceTableName"></param>
        public void RemoveAt(string sourceTableName)
        {
            this.Remove(GetBySourceTable(sourceTableName));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataSetTableName"></param>
        /// <returns></returns>
        public object this[string dataSetTableName]
        {
            get
            {
                return GetByDataSetTable(dataSetTableName);
            }
            set
            {
                TableMapping val = (TableMapping)value;
                Predicate<ITableMapping> predicate = new Predicate<ITableMapping>(
                    delegate(ITableMapping mapping) { return mapping.DataSetTable == dataSetTableName; }
                    );
                int i = this.FindIndex(predicate);
                base[i] = val;
            }
        }

        #endregion
    }
}
